﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
using WPS.Code.Code;
using WPS.Code.Utils;
using WPS.Log;
using WPS.SDK.model.yunfile;
using WPS.SDK.openapi;

namespace WPS.Web.Controllers
{
    public class WPSYunFileController : ApiController
    {
        #region 获取剩余空间
        [HttpGet, Route("wpsyunfile/remaining")]
        public JsonResult<Response> GetRemainingSpace()
        {
            LogHelper.Info("进入wpsyunfile/filelist");
            try
            {
                JObject jsonObject = new JObject();
                string accessToken = string.Empty;
                HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
                if (httpContextBase == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "MS_HttpContext参数为null."));
                }
                HttpRequestBase request = httpContextBase.Request;
                if (request["accessToken"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "accessToken为空."));
                }

                accessToken = request["accessToken"].ToString();
                LogHelper.Info("accessToken：" + accessToken);
                LogHelper.Info("进入getRemainingSpace");

                RemainingSpace remainingSpace = YunFile.getRemainingSpace(accessToken);
                LogHelper.Info("离开getRemainingSpace");
                if (remainingSpace == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "fileList is null"));
                }
                string remainingObject = JsonConvert.SerializeObject(remainingSpace);
                jsonObject["remaining"] = remainingObject;
                LogHelper.Info("离开wpsyunfile/remaining");
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Success, jsonObject, ""));
            }
            catch (Exception e)
            {
                LogHelper.Error("wpsyunfile/filelist：" + e.Message + "<br/>" + e.StackTrace);
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", e.Message));
            }
        }

        #endregion

        #region  获取文件列表
        [HttpGet, Route("wpsyunfile/filelist")]
        public JsonResult<Response> GetYunFileList()
        {
            LogHelper.Info("进入wpsyunfile/filelist");
            try
            {
                JObject jsonObject = new JObject();
                string accessToken = string.Empty;
                string parentid = "0";
                string order = string.Empty;
                string orderby = string.Empty;
                string offset = string.Empty;
                string count = string.Empty;
                HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
                if (httpContextBase == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "MS_HttpContext参数为null."));
                }
                HttpRequestBase request = httpContextBase.Request;
                if (request["accessToken"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "accessToken为空."));
                }
                if (request["parentid"] != null)
                {
                    parentid = request["parentid"].ToString();
                }
                accessToken = request["accessToken"].ToString();
                LogHelper.Info("accessToken：" + accessToken);
                LogHelper.Info("进入getAppFileList");

                FileList fileList = YunFile.getAppFileList(accessToken, parentid, order, orderby, offset, count);
                LogHelper.Info("离开getAppFileList");
                if (fileList == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "fileList is null"));
                }
                string fileListObject = JsonConvert.SerializeObject(fileList);
                jsonObject["filelist"] = fileListObject;
                LogHelper.Info("离开wpsyunfile/filelist");
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Success, jsonObject, ""));
            }
            catch (Exception e)
            {
                LogHelper.Error("wpsyunfile/filelist：" + e.Message + "<br/>" + e.StackTrace);
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", e.Message));
            }
        }
        #endregion

        #region 创建文件夹
        [HttpPost, Route("wpsyunfile/createfolder")]
        public JsonResult<Response> CreateFolder()
        {
            LogHelper.Info("进入wpsyunfile/createfolder");
            try
            {
                JObject jsonObject = new JObject();
                string accessToken = string.Empty;
                string parentid = "0";
                string name = string.Empty;

                HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
                if (httpContextBase == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "MS_HttpContext参数为null."));
                }
                HttpRequestBase request = httpContextBase.Request;
                if (request["accessToken"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "accessToken为空."));
                }
                if (request["name"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "name为空."));
                }
                if (request["parentid"] != null)
                {
                    parentid = request["parentid"].ToString();
                }
                accessToken = request["accessToken"].ToString();
                name = request["name"].ToString();
                LogHelper.Info("accessToken：" + accessToken + "<br/>name：" + name);
                LogHelper.Info("进入createFolder");
                CreateFolder createFolder = YunFile.createFolder(accessToken, parentid, name);
                LogHelper.Info("离开createFolder");
                if (createFolder == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "createFolder is null"));
                }
                string createFolderObject = JsonConvert.SerializeObject(createFolder);
                jsonObject["createFolder"] = createFolderObject;
                LogHelper.Info("离开wpsyunfile/createfolder");
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Success, jsonObject, ""));
            }
            catch (Exception e)
            {
                LogHelper.Error("wpsyunfile/createfolder：" + e.Message + "<br/>" + e.StackTrace);
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", e.Message));
            }
        }
        #endregion

        #region 上传文件
        [HttpPost, Route("wpsyunfile/uploadfile")]
        public JsonResult<Response> UploadFile()
        {
            LogHelper.Info("进入wpsyunfile/uploadfile");
            try
            {
                JObject jsonObject = new JObject();
                string accessToken = string.Empty;
                string parentid = "0";
                string name = string.Empty;

                HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
                if (httpContextBase == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "MS_HttpContext参数为null."));
                }
                HttpRequestBase request = httpContextBase.Request;
                if (request["accessToken"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "accessToken为空."));
                }
                //if (request["name"] == null)
                //{
                //    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "name为空."));
                //}
                if (request["parentid"] != null)
                {
                    parentid = request["parentid"].ToString();
                    LogHelper.Info("parentid："+ parentid);
                }
                accessToken = request["accessToken"].ToString();
                //   name = request["name"].ToString();
                LogHelper.Info("accessToken：" + accessToken + "<br/>name：" + name);
                LogHelper.Info("进入uploadfile");
                if (request.Files.Count > 0)
                {
                    HttpPostedFileBase file = request.Files[0];
                    if (file.ContentLength==0)
                    {
                        return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "不允许上传空文件！"));
                    }
                    FileInfo uploadFile = YunFile.uploadFile(accessToken, Convert.ToInt64(parentid), file);
                    if (uploadFile == null)
                    {
                        return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "uploadFile is null"));
                    }
                    LogHelper.Info("离开uploadfile");
                    string createFolderObject = JsonConvert.SerializeObject(uploadFile);
                    jsonObject["uploadfile"] = createFolderObject;
                }
                else
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "request.Files is null"));
                }
                LogHelper.Info("离开wpsyunfile/uploadfile");
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Success, jsonObject, ""));
            }
            catch (Exception e)
            {
                LogHelper.Error("wpsyunfile/uploadfile：" + e.Message + "<br/>" + e.StackTrace);
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", e.Message));
            }
        }
        #endregion

        # region 获取分享文件链接
        [HttpGet, Route("wpsyunfile/filelinkinfo")]
        public JsonResult<Response> GetFileLink()
        {
            LogHelper.Info("进入wpsyunfile/filelinkinfo");
            try
            {
                JObject jsonObject = new JObject();
                string accessToken = string.Empty;
                string fileid = string.Empty;
                string permission = "write";//权限：write/read,默认read
                string period = "-1";//有效时长：-1(永久) / 604800(7天) / 2592000(30天)，默认-1
                HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
                if (httpContextBase == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "MS_HttpContext参数为null."));
                }
                HttpRequestBase request = httpContextBase.Request;
                if (request["accessToken"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "accessToken为空."));
                }
                if (request["fileid"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "fileid为空."));
                }
                if (request["permission"] != null)
                {
                    permission = request["permission"].ToString();
                }
                accessToken = request["accessToken"].ToString();
                fileid = request["fileid"].ToString();

                LogHelper.Info("accessToken：" + accessToken + "<br/>fileid：" + fileid);
                LogHelper.Info("进入LinkInfo");
                LinkInfo linkInfo = YunFile.getFileLinkInfo(accessToken, fileid, permission, period);
                LogHelper.Info("离开LinkInfo");
                if (linkInfo == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "filelinkinfo is null"));
                }
                string linkInfoObject = JsonConvert.SerializeObject(linkInfo);
                jsonObject["linkInfo"] = linkInfoObject;
                LogHelper.Info("离开wpsyunfile/filelinkinfo");
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Success, jsonObject, ""));
            }
            catch (Exception e)
            {
                LogHelper.Error("wpsyunfile/filelinkinfo：" + e.Message + "<br/>" + e.StackTrace);
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", e.Message));
            }
        }
        #endregion

        #region 获取下载文件链接
        [HttpGet, Route("wpsyunfile/downloadlink")]
        public JsonResult<Response> GetDownLoadLink()
        {
            LogHelper.Info("进入wpsyunfile/downloadlink");
            try
            {
                JObject jsonObject = new JObject();
                string accessToken = string.Empty;
                string fileid = string.Empty;
             
                HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
                if (httpContextBase == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "MS_HttpContext参数为null."));
                }
                HttpRequestBase request = httpContextBase.Request;
                if (request["accessToken"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "accessToken为空."));
                }
                if (request["fileid"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "fileid为空."));
                }
               
                accessToken = request["accessToken"].ToString();
                fileid = request["fileid"].ToString();

                LogHelper.Info("accessToken：" + accessToken + "<br/>fileid：" + fileid);
                LogHelper.Info("进入downloadlink");
                FileDownloadUrl fileDownloadUrl = YunFile.getFileDowloadUrl(accessToken, fileid);
                LogHelper.Info("离开downloadlink");
                if (fileDownloadUrl == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "FileDownloadUrl is null"));
                }
                string fileDownloadUrlObject = JsonConvert.SerializeObject(fileDownloadUrl);
                jsonObject["downloadUrl"] = fileDownloadUrlObject;
                LogHelper.Info("离开wpsyunfile/downloadlink");
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Success, jsonObject, ""));
            }
            catch (Exception e)
            {
                LogHelper.Error("wpsyunfile/filelinkinfo：" + e.Message + "<br/>" + e.StackTrace);
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", e.Message));
            }

        }
        #endregion

        #region 文件删除
        [HttpGet, Route("wpsyunfile/deletefile")]
        public JsonResult<Response> DeleteFile()
        {
            LogHelper.Info("进入wpsyunfile/deletefile");
            try
            {
                JObject jsonObject = new JObject();
                string accessToken = string.Empty;
                string fileid = string.Empty;
               
                HttpContextBase httpContextBase = (HttpContextBase)Request.Properties["MS_HttpContext"];
                if (httpContextBase == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "MS_HttpContext参数为null."));
                }
                HttpRequestBase request = httpContextBase.Request;
                if (request["accessToken"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "accessToken为空."));
                }
                if (request["fileid"] == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, null, "fileid为空."));
                }
             
                accessToken = request["accessToken"].ToString();
                fileid = request["fileid"].ToString();

                LogHelper.Info("accessToken：" + accessToken + "<br/>fileid：" + fileid);
                LogHelper.Info("进入deletefile");
                FileDelete fileDelete = YunFile.fileDelete(accessToken, fileid);
                LogHelper.Info("离开deletefile");
                if (fileDelete == null)
                {
                    return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", "deletefile is null"));
                }
                string fileDeleteObject = JsonConvert.SerializeObject(fileDelete);
                jsonObject["fileDelete"] = fileDeleteObject;
                LogHelper.Info("离开wpsyunfile/deletefile");
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Success, jsonObject, ""));
            }
            catch (Exception e)
            {
                LogHelper.Error("wpsyunfile/deletefile：" + e.Message + "<br/>" + e.StackTrace);
                return Json<Response>(ApiUtils.DoResponse(ResponseCode.Fail, "", e.Message));
            }
        }

        #endregion
    }
}